# -*- coding: utf-8 -*-
from MsgProcess import MsgProcess, MsgType
import RPi.GPIO as GPIO
import time

P_SERVO = 15 # GPIO端口号，根据实际修改
fPWM = 50  # Hz (软件PWM方式，频率不能设置过高)

class Steering_engine(MsgProcess):

    def setDirection(self,pwm,direction):
        duty = 10 / 180 * direction + 2
        pwm.ChangeDutyCycle(duty)
        time.sleep(1)

    def Text(self, message):  
        GPIO.setmode(GPIO.BOARD)
        GPIO.setup(P_SERVO, GPIO.OUT)
        pwm = GPIO.PWM(P_SERVO, fPWM)
        pwm.start(0)

        data = message["Data"]
        Triggers= [str(x) for x in range(181)]
        key  = [x  for x in Triggers if x in data ]
        if  key:
            self.setDirection(pwm, int(key[-1]) )
            GPIO.cleanup()
            self.say("完成旋转{0}度".format(int(key[-1])))
        else:
            self.say("你可以说旋转180度或者旋转0.0度")
        self.Stop()

